import { Column, Entity, JoinColumn, ManyToOne, OneToMany, OneToOne, PrimaryColumn, PrimaryGeneratedColumn, Tree, TreeChildren, TreeParent } from "typeorm";

@Entity('test_user')
export class UserEntity {

  // 用户ID
  @PrimaryGeneratedColumn()
  id: number;

  // 用户昵称
  @Column({ name: 'user_name' })
  userName: string;

  // 所属组织ID
  @Column({ name: 'org_id' })
  orgId: number;

  // 账号
  @Column({ name: 'login_name' })
  loginName: string;

  @Column({ select: false })
  password: string;

  @Column({ type: 'bool', select: false })
  delete_flag: boolean;

  // 定义与关联表的一对多关系
  @OneToMany(() => UserRoleId, (userRole) => userRole.userId)
  users: UserRoleId[];

  // 用户角色列表
  roles: RoleEntity[];

  // 菜单
  menuList: MenuEntity[]

  // 操作按钮列表
  handleList: string[]
}

@Entity('test_user_role_id')
export class UserRoleId {
  // 主键
  @PrimaryGeneratedColumn()
  id: number;

  // 用户Id(多对一)
  @Column({ name: 'user_id' })
  @ManyToOne(() => UserEntity, user => user.users)
  @JoinColumn({ name: 'user_foreign' })
  userId: number;

  // 角色Id(多对一)
  @Column({ name: 'role_id' })
  @ManyToOne(() => RoleEntity, role => role.roles)
  @JoinColumn({ name: 'role_foreign' })
  roleId: number;
}

@Entity('test_role')
export class RoleEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ name: 'role_name' })
  roleName: string;

  @Column({ type: 'bool', select: false })
  delete_flag: boolean;

  // 定义与用户关联表一对多的关系
  @OneToMany(() => UserRoleId, userRole => userRole.roleId)
  roles: UserRoleId[];

  // 定义与菜单关联表一对多的关系
  @OneToMany(() => RoleMenuEntity, roleMemu => roleMemu.roleId)
  menus: RoleMenuEntity[]
}

@Entity('test_role_menu_id')
export class RoleMenuEntity {

  @PrimaryGeneratedColumn()
  id: number;

  // 角色id
  @Column({ name: 'role_id' })
  @ManyToOne(() => RoleEntity, role => role.roles)
  @JoinColumn({ name: 'role_foreign' })
  roleId: number;

  // 菜单id
  @Column({ name: 'menu_id' })
  @ManyToOne(() => MenuEntity, menu => menu.menuId)
  @JoinColumn({ name: 'menu_foreign' })
  menuId: number;
}

@Entity('test_menu_closure')
export class MenuClosure {

  @PrimaryGeneratedColumn()
  id: number;

  // 菜单id
  @Column({ name: 'descendant_id' })
  @OneToOne(() => MenuEntity, menu => menu.parent)
  @JoinColumn({ name: 'test_menu_closure_descendant_id' })
  descendantId: number;

  // 上级目录id
  @Column({ name: 'ancestor_id' })
  ancestorId: number;
}


// @Tree('closure-table', {
//   closureTableName: "test_menu",
//   ancestorColumnName: () => "ancestor_id",
//   descendantColumnName: () => "descendant_id",
// })
@Entity('test_menu')
export class MenuEntity {
  // 菜单ID
  @PrimaryGeneratedColumn()
  id: number;

  // 菜单名称
  @Column({ name: 'menu_name' })
  menuName: string;

  // 路由地址
  @Column({ name: 'router_path' })
  path: string;

  // 是否启用缓存
  @Column({ name: 'keep_alive' })
  keepAlive: boolean;

  // 路由名称
  @Column({ name: 'router_name' })
  routerName: string;

  // 入口文件地址
  @Column({ name: 'component' })
  component: string;

  // icon
  @Column()
  icon: string;

  // 节点类型
  @Column()
  type: string;

  @Column({ select: false })
  parentId: number;

  // 子集菜单
  // @TreeChildren()
  // children: MenuEntity[]

  // 定义关联表外键
  @OneToOne(() => MenuClosure, menuClosure => menuClosure.descendantId)
  parent: MenuClosure;

  // 定义菜单与角色一对多的关系
  @OneToMany(() => RoleMenuEntity, roleMemu => roleMemu.menuId)
  menuId: RoleMenuEntity[]
}